<?php

/**
 * Web page displaying one Media item.
 * 
 * @copyright (c) 2012 University of Geneva
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
 * @author Laurent Opprecht laurent@opprecht.info
 */
class EMediaView
{

    /**
     *
     * @param type $course
     * @param type $cm
     * @param type $emedia
     * 
     * @return EMediaView 
     */
    public static function create($course, $cm, $emedia)
    {
        return new self($course, $cm, $emedia);
    }

    protected $course;
    protected $cm;
    protected $emedia;

    function __construct($course, $cm, $emedia)
    {
        $this->course = $course;
        $this->cm = $cm;
        $this->emedia = $emedia;
    }

    function display_header()
    {
        global $PAGE, $OUTPUT;

        $course = $this->course;
        $cm = $this->cm;
        $emedia = $this->emedia;

        $PAGE->set_title($course->shortname . ': ' . $emedia->title);
        $PAGE->set_heading($course->fullname);
        $PAGE->set_activity_record($emedia);
        echo $OUTPUT->header();
    }

    /**
     * Print url heading.
     * 
     */
    function display_heading()
    {
        global $OUTPUT;

        $course = $this->course;
        $cm = $this->cm;
        $emedia = $this->emedia;
        $options = $emedia->options;

        $printheading = isset($options['printheading']) ? $options['printheading'] : true;
        if (!$printheading)
        {
            return;
        }
        
        $url = $emedia->url;
        $text = '<a href="'. $url .'">'. format_string($emedia->title) . '</a>';

        echo $OUTPUT->heading($text, 2, 'main emedia heading');
    }

    /**
     * Print emedia introduction.
     * @return void
     */
    function display_intro()
    {
        global $OUTPUT;
        $course = $this->course;
        $cm = $this->cm;
        $emedia = $this->emedia;
        $options = $emedia->options;

        $printintro = isset($options['printintro']) ? $options['printintro'] : true;
        if (!$printintro)
        {
            return;
        }

        if (trim(strip_tags($emedia->intro)))
        {
            echo $OUTPUT->box_start('emedia intro');
            echo format_module_intro('emedia', $emedia, $cm->id);
            echo $OUTPUT->box_end();
            return;
        }

        $description = $emedia->description;
        if (trim(strip_tags($description)))
        {
            echo $OUTPUT->box_start('emedia intro');
            echo $description;
            echo $OUTPUT->box_end();
            return;
        }
    }

    function display_emedia()
    {
        $emedia = $this->emedia;
        $cm = $this->cm;

        $url = EMedia::filter_url($emedia->url);
        if (empty($url))
        {
            $message = EMedia::get_string('invalidurl');
            $link = new moodle_url('/course/view.php', array('id' => $cm->course));
            notice($message, $link);
            die;
        }
        else
        {
            echo '<div class="emedia content">';
            echo $emedia->snippet;
            echo '</div>';
        }
    }

    function display()
    {
        $this->display_header();
        $this->display_heading();
        $this->display_intro();
        $this->display_emedia();
        $this->display_footer();
    }

    function display_footer()
    {
        global $OUTPUT;
        echo $OUTPUT->footer();
    }

}